
package net.vinote.smart.platform.service.servlet;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.vinote.smart.platform.service.SRE;
import net.vinote.smart.platform.service.bean.DataWindowCatalog;
import net.vinote.smart.platform.service.bean.User;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


/**
 * 
 * 处理文件上传下载
 * 
 * @author 郑俊伟
 * 
 */
public class FileUploadServlet extends HttpServlet
{
	private static final long serialVersionUID = -4438585702968947282L;

	public static final int SUCCESS = 200;

	public static final int ERROR = 500;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		HttpSession session = req.getSession();
		// 文件保存目录路径
		User user = (User) req.getSession().getAttribute("user");
		String savePath = getServletContext().getRealPath("/") + File.separator
				+ "WEB-INF/" + File.separator + "attache" + File.separator
				+ user.getUserID();
		
		//上传文件类型:图片、附件
		String type=req.getParameter("type");
		if(type==null){
			
		}
		// 对同一用户的图片资源进行按表引用分类
		String doNo = req.getParameter("doNo");
		DataWindowCatalog catalog = SRE.getDataWindowCatalog(doNo);
		if (catalog != null)
		{
			savePath += File.separator + catalog.getMainTable().toLowerCase();
		}
		
		File saveDirFile = new File(savePath);
		if (!saveDirFile.exists())
		{
			saveDirFile.mkdirs();
		}
		FileItemFactory fileItemFactory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(fileItemFactory);
		try
		{
			Iterator<?> iterator = upload.parseRequest(req).iterator();
			while (iterator.hasNext())
			{
				FileItem item = (FileItem) iterator.next();
				String name = item.getName();
				if (item.getSize() == 0)
				{
					throw new NullPointerException("file is null or empty!");
				}
				String fileExt = name.substring(name.lastIndexOf(".") + 1)
						.toLowerCase();
				SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
				String newFileName = df.format(new Date()) + "_"
						+ new Random().nextInt(1000) + "." + fileExt;
				File uploadedFile = new File(savePath, newFileName);
				item.write(uploadedFile);
				session.setAttribute("path", "attache/" + newFileName);
			}
			session.setAttribute("result", SUCCESS);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			session.setAttribute("result", ERROR);
		}
		resp.sendRedirect((String) session.getAttribute("returnURL"));
	}

}
